#include <QDebug>
#include <QDateTime>
#include <QFile>
#include <io.h>
#include "cproxyconnect.h"
#include "CInterface_global.h"

char* szBuffer   = (char*)malloc(80*1024*1024);
const int CommandHeadLen = 4;
CProxyConnect& CProxyConnect::Instance() {
    static CProxyConnect instance;
    return instance;
}

void CProxyConnect::Reset() {
    proxying = 0;
}

static qint64   updateTime;
static int      updateStep;
void CProxyConnect::run() {
    while(true) {
        QDateTime dt = QDateTime::currentDateTime();
        //超时重连平板
        if(updateTime + updateStep * 2 < dt.toMSecsSinceEpoch()) {
            //连接不成功过半秒重连
            if(!CreatePadConnect()) { msleep(500); continue; }
        }
        //连接成功马上读请求
        updateTime = dt.toMSecsSinceEpoch();

        //读请求数据
        int cmdLen = ReadCommand();
        if(cmdLen == 0) { updateTime = 0; continue; }

        //向服务端要求数据
        int resultLen = ServerDoCommand(cmdLen);
        if(resultLen == 0) {
            updateTime = 0; continue;
        }

        //回写到平板
        if(!usbConnect.Write((char*)&resultLen, 4)) { updateTime = 0; continue; }
        if(!usbConnect.Write(szBuffer, resultLen)) { updateTime = 0; continue; }

        msleep(100);
    }
}

bool CProxyConnect::ProxyStarted() {
    return proxying == 1;
}

CProxyConnect::CProxyConnect() : proxying(0) {
    QString strTimeStep = QString::fromLocal8Bit(CsGetConfig("监控数据上传间隔"));
    if(strTimeStep.isEmpty())
        updateStep = 10000;
    else
        updateStep = strTimeStep.toLong() * 1000;
}

extern void    StartAdbProxy(bool reconnect = false);
bool CProxyConnect::CreatePadConnect() {
    proxying = 0;

    qDebug() << "准备连接USB";
    usbConnect = new QTcpSocket();
    if(!usbConnect.Connect("127.0.0.1", 9906)) {
        StartAdbProxy(true);
        return false;
    }

    CopyData_Generic cd = { CommandID::SOCKET_PROXY };
    int len = sizeof(cd);

    if(!usbConnect.Write((char*)&len, CommandHeadLen))
        return false;

    if(!usbConnect.Write((char*)&cd, len))
        return false;

    if(!usbConnect.Read((char*)&proxying, CommandHeadLen, updateStep))
        return false;

    qDebug() << "连接USB代理";
    return proxying;
}

int CProxyConnect::ServerDoCommand(int cmdLen) {
    CSocket toPjServier;

    const char* ip = CsGetConfig("评价服务器IP");
    int port = QString::fromLocal8Bit(CsGetConfig("评价服务器Port")).toInt();
    if(!toPjServier.Connect(ip, port)) return 0;

    if(!toPjServier.Write((char*)&cmdLen, 4)) return 0;
    if(!toPjServier.Write(szBuffer, cmdLen)) return 0;

    int resultLen = 0;
    if(!toPjServier.Read((char*)&resultLen, 4, updateStep)) return 0;
    if(!toPjServier.Read(szBuffer, resultLen, updateStep)) return 0;

    return resultLen;
}

int CProxyConnect::ReadCommand() {
    int size = 0;
    usbConnect.Read((char*)&size, 4, updateStep);
    if(size <= 0) return 0;

    if(!usbConnect.Read(szBuffer, size, updateStep)) return 0;

    szBuffer[size] = '\0';

    return size;
}

